<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">

	<title>文件列表</title>

	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link rel="stylesheet" href="${ctxPath}/lib/layui/css/layui.css" media="all">
	<link rel="stylesheet" href="${ctxPath}/css/public.css" media="all">

</head>

<body>
	<div class="layuimini-container">
		<div class="layuimini-main">
			<div class="layui-card-body">
				<div>
					<button id="file-btn-refresh" class="layui-btn layui-btn-sm layui-btn-primary icon-btn">
						<i class="layui-icon">&#xe669;</i>刷新
					</button>
					<button id="file-btn-upload" class="layui-btn layui-btn-sm layui-btn-normal icon-btn">
						<i class="layui-icon">&#xe681;</i>上传文件
					</button>
				</div>
				<div id="file-list-group"></div>
			</div>

			<a href="" id="down_a"></a>

			<!-- 表格头部功能区 -->
			<script type="text/html" id="toolbarDemo">
	            <div class="layui-btn-container">
	                <button class="layui-btn layui-btn-sm layui-bg-blue" id="file-btn-upload" lay-event="add"> 上传新文件 </button>
	                <button class="layui-btn layui-btn-sm layui-bg-orange" lay-event="batchDelete"> 批量删除 </button>
	                <button class="layui-btn layui-btn-sm layui-bg-red" lay-event="deleteAll"> 全部删除 </button>
	            </div>
	        </script>

			<!-- 数据表格 -->
			<table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

			<!-- 表格右侧功能区 -->
			<script type="text/html" id="currentTableBar">
				<a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="download">下载</a>
				<a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">删除</a>
	        </script>

			<!-- 状态 -->
			<script type="text/html" id="statusTpl">
				<input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="statusTpl" {{ d.status == 1 ? 'checked' : '' }}>
			</script>

		</div>
	</div>

	<script src="${ctxPath}/lib/layui/layui.js" charset="utf-8"></script>
	<script src="${ctxPath}/js/core.util.js" charset="utf-8"></script>
	<script src="${ctxPath}/js/lay-config.js" charset="utf-8"></script>

	<script type="text/javascript">
		layui.use(['form', 'table', 'notice', 'upload'], function () {
			var $ = layui.jquery, form = layui.form, table = layui.table, notice = layui.notice, upload = layui.upload;

			table.render({
				elem: '#currentTableId',
				url: '/sys/file/list',
				//toolbar: '#toolbarDemo',
				cols: [[
					{type: "checkbox", width: 50},
					{field: 'id', width: 200, title: 'ID', sort: true},
					{field: 'fileOriginName', title: '文件名称'},
					{field: 'fileObjectName', title: '存储名称'},
					{field: 'fileSuffix', title: '文件后缀', sort: true},
					{field: 'filePath', title: '存储路径', sort: true},
					{field: 'createTime', width: 168, title: '入库时间', align: "center", templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
					{title: '操作', width: 150, toolbar: '#currentTableBar', align: "center", fixed: 'right', }
				]],
				limits: [50, 100, 200, 300, 500, 700, 1000, 1500, 5000, 10000],
				limit: 50,
				page: true,
				skin: 'row|line',
			});

			// 上传
			upload.render({
				elem: '#file-btn-upload',
				accept: 'file',
				url: '/sys/file/upload',
				done: function (res) {
					if (res.code == 0) {
						layer.msg(res.msg, {icon: 1, time: 1000});
						table.reload('currentTableId');
					} else {
						layer.msg(res.msg, {icon: 2, time: 1000});
					}
				}
			});

			$("#file-btn-refresh").on("click", function () {
				table.reload('currentTableId');
			});

			// 表格 右侧操作按钮
			table.on('tool(currentTableFilter)', function (obj) {
				var data = obj.data;
				if (obj.event === 'delete') {
					layer.confirm('确定删除此数据吗？', {btn: ['确定', '取消']}, function (index) {
						sendAjax("/sys/file/delete?id=" + data.id, null, function (rest) {
							notice.msg(rest.msg, {icon: 1});
							layer.close(index);
							table.reload('currentTableId');
						});
					}, function () {
						// 取消动作
					});
				} else if (obj.event === 'download') {
					$("#down_a").attr("href", "/sys/file/download?id=" + data.id).click();

					document.getElementById("down_a").click();
					/*
					sendAjax("/sys/file/download?id=" + data.id, null, function (rest) {
						notice.msg(rest.msg, {icon: 1});
						table.reload('currentTableId');
					});
					*/
				}
			});



		});
	</script>
</body>

</html>